
package ca.canucksoftware.themebuilder;

import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;

public class CompatibleDevicesVersions extends javax.swing.JDialog {
    private ZipTheme theme;
    private List<String> versions;

    public CompatibleDevicesVersions(java.awt.Frame parent, ZipTheme zipTheme) {
        super(parent);
        initComponents();
        theme = zipTheme;
        jCheckBox2.setSelected(theme.devices.contains("pre"));
        jCheckBox1.setSelected(theme.devices.contains("veer"));
        jCheckBox4.setSelected(theme.devices.contains("pre3"));
        jCheckBox3.setSelected(theme.devices.contains("touchpad"));
        jCheckBox5.setSelected(theme.devices.contains("emulator"));
        versions = theme.controller.list();
        jList1.setListData(versions.toArray());
        getContentPane().requestFocus();
    }

    public void closeAddContent() {
        dispose();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox5 = new javax.swing.JCheckBox();
        jButton2 = new javax.swing.JButton();
        jLayeredPane3 = new javax.swing.JLayeredPane();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ca.canucksoftware.themebuilder.WebOSThemeBuilderApp.class).getContext().getResourceMap(CompatibleDevicesVersions.class);
        setTitle(resourceMap.getString("title")); // NOI18N
        setBackground(resourceMap.getColor("transfer.background")); // NOI18N
        setForeground(resourceMap.getColor("transfer.foreground")); // NOI18N
        setIconImage(null);
        setModal(true);
        setName("transfer"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLayeredPane1.setName("jLayeredPane1"); // NOI18N

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), resourceMap.getString("jLayeredPane2.border.title"))); // NOI18N
        jLayeredPane2.setName("jLayeredPane2"); // NOI18N

        jCheckBox3.setText(resourceMap.getString("jCheckBox3.text")); // NOI18N
        jCheckBox3.setName("jCheckBox3"); // NOI18N
        jCheckBox3.setBounds(20, 150, 160, 30);
        jLayeredPane2.add(jCheckBox3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jCheckBox4.setText(resourceMap.getString("jCheckBox4.text")); // NOI18N
        jCheckBox4.setName("jCheckBox4"); // NOI18N
        jCheckBox4.setBounds(20, 110, 160, 30);
        jLayeredPane2.add(jCheckBox4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N
        jCheckBox1.setBounds(20, 70, 160, 30);
        jLayeredPane2.add(jCheckBox1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jCheckBox2.setText(resourceMap.getString("jCheckBox2.text")); // NOI18N
        jCheckBox2.setName("jCheckBox2"); // NOI18N
        jCheckBox2.setBounds(20, 30, 160, 30);
        jLayeredPane2.add(jCheckBox2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jCheckBox5.setText(resourceMap.getString("jCheckBox5.text")); // NOI18N
        jCheckBox5.setName("jCheckBox5"); // NOI18N
        jCheckBox5.setBounds(20, 190, 160, 30);
        jLayeredPane2.add(jCheckBox5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBounds(10, 10, 200, 240);
        jLayeredPane1.add(jLayeredPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton2.setFont(resourceMap.getFont("jButton2.font")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.setBounds(170, 260, 140, 25);
        jLayeredPane1.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), resourceMap.getString("jLayeredPane3.border.title"))); // NOI18N
        jLayeredPane3.setName("jLayeredPane3"); // NOI18N

        jButton3.setBackground(resourceMap.getColor("jButton3.background")); // NOI18N
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jButton3.setBounds(180, 30, 30, 30);
        jLayeredPane3.add(jButton3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setEnabled(false);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jButton4.setBounds(40, 200, 110, 25);
        jLayeredPane3.add(jButton4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setName("jList1"); // NOI18N
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jScrollPane1.setBounds(20, 30, 150, 160);
        jLayeredPane3.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setBackground(resourceMap.getColor("jButton1.background")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(180, 80, 30, 30);
        jLayeredPane3.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane3.setBounds(220, 10, 230, 240);
        jLayeredPane1.add(jLayeredPane3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 458, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    }//GEN-LAST:event_formWindowOpened

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
    }//GEN-LAST:event_formWindowActivated

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    }//GEN-LAST:event_formWindowClosed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if((jCheckBox2.isSelected() || jCheckBox1.isSelected() || jCheckBox4.isSelected() ||
                jCheckBox3.isSelected() || jCheckBox5.isSelected()) && versions.size()>0) {
            theme.devices.clear();
            if(jCheckBox2.isSelected()) {
                theme.devices.add("pre");
            }
            if(jCheckBox1.isSelected()) {
                theme.devices.add("veer");
            }
            if(jCheckBox4.isSelected()) {
                theme.devices.add("pre3");
            }
            if(jCheckBox3.isSelected()) {
                theme.devices.add("touchpad");
            }
            if(jCheckBox5.isSelected()) {
                theme.devices.add("emulator");
            }
            String curr;
            List<String> prevVersions = theme.controller.list();
            for(int i=0; i<prevVersions.size(); i++) {
                curr = prevVersions.get(i);
                if(!versions.contains(curr)) {
                    System.out.println("Removing " + curr);
                    if(curr.contains("->")) {
                        theme.controller.removeAlias(curr);
                    } else {
                        theme.controller.removeVersion(curr);
                    }
                }
            }
            for(int i=0; i<versions.size(); i++) {
                curr = versions.get(i);
                if(!prevVersions.contains(curr)) {
                    System.out.println("Adding " + curr);
                    if(curr.contains("->")) {
                        String ver = curr.substring(0, curr.indexOf(" "));
                        String realVer = curr.substring(curr.lastIndexOf(" ")+1);
                        theme.controller.addAlias(ver, realVer);
                    } else {
                        theme.controller.addVersion(curr);
                    }
                }
            }
            closeAddContent();
        } else {
            JOptionPane.showMessageDialog(rootPane, "ERROR: At least 1 device and 1 webOS version are rrequired.");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        String input = JOptionPane.showInputDialog("Enter webOS version number:\n(eg. 2.1.0)");
        jLayeredPane1.requestFocus();
        if(input!=null && input.trim().length()>0) {
            versions.add(input.trim());
            jList1.setListData(versions.toArray());
        }
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        jLayeredPane1.requestFocus();
        if(jList1.getSelectedIndex()>-1) {
            String item = (String) jList1.getSelectedValue();
            if(!item.contains("->")) {
                //remove any associated aliases
                for(int j=0; j<versions.size(); j++) {
                    if(versions.get(j).endsWith(" -> " + item)) {
                        versions.remove(j);
                        j--;
                    }
                }
            }
            versions.remove(jList1.getSelectedIndex());
            jList1.setListData(versions.toArray());
        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        if(jList1.getSelectedIndex()>-1) {
            if(!((String)jList1.getSelectedValue()).contains("->")) {
                jButton4.setEnabled(true);
            } else {
                jButton4.setEnabled(false);
            }
        } else {
            jButton4.setEnabled(false);
        }
    }//GEN-LAST:event_jList1ValueChanged

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        String item = (String) jList1.getSelectedValue();
        String input = JOptionPane.showInputDialog("Enter webOS version number for the alias:\n(eg. 2.1.0)");
        jLayeredPane1.requestFocus();
        if(input!=null && input.trim().length()>0) {
            String verString = input.trim() + " -> " + item;
            if(!versions.contains(verString)) {
                versions.add(verString);
                jList1.setListData(versions.toArray());
            }
            jList1.setSelectedIndex(-1);
        }
    }//GEN-LAST:event_jButton4ActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JLayeredPane jLayeredPane3;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
